﻿using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyTuyenSinh.BussinessLayer;
using QuanLyTuyenSinh.Properties;

namespace QuanLyTuyenSinh.PresentationLayer
{
    public partial class frmUniversity : Office2007Form
    {
        private int ma;

        public frmUniversity()
        {
            InitializeComponent();
        }
        private void HideControl(bool hien)
        {
            cmdThem.Visible = hien;
            cmdLuu.Visible = hien;
            cmdXoa.Visible = hien;
            cmdThem.Enabled = true;
            cmdXoa.Enabled = true;
        }
        private void FrmTruongDhLoad(object sender, EventArgs e)
        {
            try
            {
                DataTable table = Operator.Operator.GetListUniversity();
                if (table.Rows.Count > 0)
                {
                    GrTruongDH.DataSource = table;
                }
                HideControl(false);
                cmdThem.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void CmdThemClick(object sender, EventArgs e)
        {
            txtMatruong.Focus();
            txtMatruong.Clear();
            txtTentruong.Clear();
            txtDiachi.Clear();
            HideControl(true);
            ma = 0;
            cmdXoa.Visible = false;
        }


        private void CmdLuuClick(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtMatruong.Text.Trim()))
                {
                    MessageBox.Show("Chưa nhập: Mã trường");
                    return;
                }
                var ethnic = new University();
                ethnic.UniversityDisplay = txtMatruong.Text.Trim();
                ethnic.UniversityName = txtTentruong.Text.Trim();
                ethnic.UniversityAddress = txtDiachi.Text.Trim();
                ethnic.UniversityMobile = txtMobie.Text.Trim();
                DataTable listEthnic = Operator.Operator.GetListUniversityById(ma);
                if (listEthnic.Rows.Count > 0)
                {
                    ethnic.Id = ma;
                    int m = ethnic.Update(ethnic);
                    if (m > 0)
                    {
                        FrmTruongDhLoad(sender, e);
                    }
                }
                else
                {
                    int m = ethnic.Insert(ethnic);
                    if (m > 0)
                    {
                        FrmTruongDhLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void GrTruongDhCellClick(object sender, DataGridViewCellEventArgs e)
        {
            var dong = new DataGridViewRow();
            if (e.RowIndex >= 0)
            {
                dong = GrTruongDH.Rows[e.RowIndex];
                ma = int.Parse(dong.Cells[0].Value.ToString());
                txtMatruong.Text = dong.Cells[1].Value.ToString();
                txtTentruong.Text = dong.Cells[2].Value.ToString();
                txtDiachi.Text = dong.Cells[3].Value.ToString();
                txtMobie.Text = dong.Cells[4].Value.ToString();
                HideControl(true);
            }
        }


        private void CmdXoaClick(object sender, EventArgs e)
        {
            try
            {
                DialogResult DRL;
                DRL = MessageBox.Show(Resources.Delete, Resources.Header, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (DRL == DialogResult.No)
                {
                    return;
                }
                else
                {
                    var ethnic = new University();
                    ethnic.Id = ma;
                    int m = ethnic.Delete(ethnic);
                    if (m > 0)
                    {
                        FrmTruongDhLoad(sender, e);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private void FrmTruongDhKeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.S) && cmdLuu.Visible)
            {
                CmdLuuClick(sender, e);
            }
            else if (e.Control && e.KeyCode == Keys.N)
            {
                CmdThemClick(sender, e);
            }
            else if ((e.KeyCode == Keys.Delete) && (cmdXoa.Visible))
            {
                CmdXoaClick(sender, e);
            }
        }
    }
}